package madixin.t20;

import java.util.Stack;

class Solution {
    public boolean isValid(String s) {

        if (s == null) {
            return false;
        }

        Stack<Character> stack = new Stack<Character>();

        for (int i = 0; i < s.length(); i++) {

            char cur = s.charAt(i);
            switch (cur) {
                case '(': {
                    stack.push(cur);
                    break;
                }
                case '[': {
                    stack.push(cur);
                    break;
                }
                case '{': {
                    stack.push(cur);
                    break;
                }
                case ')': {
                    if (stack.isEmpty() || (!stack.isEmpty() && !stack.pop().equals('('))) {
                        return false;
                    }
                    break;
                }
                case ']': {
                    if (stack.isEmpty() || (!stack.isEmpty() && !stack.pop().equals('['))) {
                        return false;
                    }
                    break;
                }
                case '}': {
                    if (stack.isEmpty() || (!stack.isEmpty() && !stack.pop().equals('{'))) {
                        return false;
                    }
                    break;
                }
                default:
                    return false;
            }

        }

        return stack.isEmpty();
    }

    public static void main(String[] args) {

        Solution s = new Solution();
        //System.out.println(s.isValid("]"));
//        System.out.println(s.isValid("()"));
        System.out.println(s.isValid("()[]{}"));
//        System.out.println(s.isValid("(]"));
//        System.out.println(s.isValid("([)]"));
//        System.out.println(s.isValid("(a)"));
    }
}